package com.smartcruise.main.mainFragment.tools.map;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Poi;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.NormalListDialog;
import com.smartcruise.R;
import com.smartcruise.base.BaseTitleActivity;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.util.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FengChaoQun
 * on 2018/1/27 0027
 * 地图
 */

public class MapActivity extends BaseTitleActivity implements MapContract.View, AMap.OnPOIClickListener,
        AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener {

    @BindView(R.id.mapView)
    MapView mapView;
    @BindView(R.id.inputKeyWord)
    AutoCompleteTextView inputKeyWord;
    @BindView(R.id.searchButton)
    Button searchButton;

    private AMap aMap;
    private MyLocationStyle myLocationStyle;
    private MapContract.Presenter mPresenter;

    @Override
    protected void init() {
        setEnableRightSlide(false);
        mPresenter = new MapPresenterImp(this);
        mPresenter.start();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mapView.getMap();
            setUp();
        }
    }

    private void setUp() {
        myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
        aMap.setMyLocationStyle(myLocationStyle);

        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.getUiSettings().setCompassEnabled(true);//设置指南针可见
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false

        aMap.setOnPOIClickListener(this);
        aMap.setOnMarkerClickListener(this);
        aMap.setOnInfoWindowClickListener(this);

        aMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        inputKeyWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isNotEmpty(inputKeyWord)) {
                    mPresenter.getInputTips(inputKeyWord.getText().toString());
                } else {
                    aMap.clear();
                }
            }
        });
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_container_map;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        TitleBuilder titleBuilder = TitleBuilder.create(true, getString(R.string.map));
        titleBuilder.setShowRightImage(true);
        titleBuilder.setRightImageSrc(R.drawable.ic_action_scan);
        return titleBuilder;
    }

    @Override
    protected void clickOnRightImage() {
        String[] strings = new String[]{"标准地图", "卫星地图", "夜间模式", "导航模式"};
        final NormalListDialog normalListDialog = new NormalListDialog(getActivity(), strings);
        normalListDialog.title("选择地图模式");
        normalListDialog.layoutAnimation(null);
        normalListDialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
                        break;
                    case 1:
                        aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 卫星地图模式
                        break;
                    case 2:
                        aMap.setMapType(AMap.MAP_TYPE_NIGHT);//夜景地图模式
                        break;
                    case 3:
                        aMap.setMapType(AMap.MAP_TYPE_NAVI);//导航地图模式
                        break;
                }
                normalListDialog.dismiss();
            }
        });
        normalListDialog.show();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @OnClick(R.id.searchButton)
    public void onViewClicked() {
        if (StringUtils.isNotEmpty(inputKeyWord)) {
            mPresenter.search(inputKeyWord.getText().toString());
        }
    }

    @Override
    public void setInputTips(List<String> listString) {
        ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(
                getApplicationContext(),
                R.layout.item_auto_text_view, listString);
        inputKeyWord.setAdapter(aAdapter);
        aAdapter.notifyDataSetChanged();
    }

    @Override
    public AMap getAmap() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        return aMap;
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    @Override
    public void onPOIClick(Poi poi) {
        aMap.clear();
        MarkerOptions markOptiopns = new MarkerOptions();
        markOptiopns.position(poi.getCoordinate())
                .title(poi.getName()).snippet("点击开始导航");
        Marker marker = aMap.addMarker(markOptiopns);
        marker.showInfoWindow();
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        NavigateActivity.start(getActivity(), marker.getPosition(), marker.getTitle());
//        AmapNaviPage.getInstance().showRouteActivity(getActivity(), new AmapNaviParams(null), null);
    }
}
